﻿@{
    Layout = "~/Areas/Admin/Views/Shared/_LayoutWithoutMenu.cshtml";
    string toolbarItem = ViewBag.ToolbarItem ?? "add,edit,delete,reload,detail";
}

@section css{
    <style type="text/css">
    </style>
}

@section scripts
{
    <!--请在下方写此页面业务相关的脚本-->
    <script type="text/javascript" src="/content/lib/My97DatePicker/4.8/WdatePicker.js"></script>
    <script type="text/javascript" src="/content/lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="/content/lib/datatables/DataTables.js"></script>
    <script type="text/javascript" src="/content/lib/laypage/1.2/laypage.js"></script>

    <!--DataTable参数配置-->
    <script type="text/javascript">
        var rownumbers = true, singleSelect = false, ctrlSelect = true, multiSort = false, pageSize = 25;
        var grid, frozenColumns = [[]], columns = [[]], filterData = [], enableFilterData = false, editIndex = undefined, columnMenu = undefined, queryParams = {};

        var startfunction = function () {
        };
        var endfunction = function () {
        };
        var addObject = function () {
            return {};
        };
        var replaceSearchField = function (field) {
            return field;
        };

        // datatable查询参数
        var searchData = function (data) {
            return JSON.stringify(data);
        }
    </script>

    <!--参数初始化-->
    @RenderSection("paramInit", false)

    <script type="text/javascript">
    var dttable;

    $(document).ready(function () {
        dttable = $('#tb-@ViewBag.GridId')
            .DataTable({
                "ajax": {
                    "url": "@ViewBag.GridDataUrl",
                        "dataType": "json",
                        "beforeSend": function () { layer.load(2); },
                        "complete": function () { layer.closeAll('loading'); },
                        "data": searchData
                    },
                    columns: columns,
                    "order": []
                });
        });

        // 刷新
        function reload() {
            dttable.ajax.reload();
        }

        // 单击选择行
        //$('#dataTable tbody')
        //    .on('click',
        //        'tr',
        //        function() {
        //            $(this).toggleClass('selected');
        //        });

        // 获取已勾选行的编号
        function getCheckedCodes() {
            var nTrs = dttable.rows('.selected').data();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
            var codes = "";
            for (var i = 0; i < nTrs.length; i++) {
                codes = codes + nTrs[i]["Code"] + ",";
            }
            $("#codes").val(codes);
        }

        // 全部勾选
        $('#_checkall').on('click', function () {
            var check = $(this).prop("checked");
            $(".checkchild").prop("checked", check);

            if (check) {
                $('#tb-@ViewBag.GridId tbody tr').addClass('selected');
        } else {
            $('#tb-@ViewBag.GridId tbody tr').removeClass('selected');
        }

        getCheckedCodes();
    });

    // 单个勾选
    $('#tb-@ViewBag.GridId tbody').on('change', 'input[type="checkbox"]', function () {
        // If checkbox is not checked
        if (!this.checked) {
            var el = $('#_checkall').get(0);
            // If "Select all" control is checked and has 'indeterminate' property
            if (el && el.checked && ('indeterminate' in el)) {
                // Set visual state of "Select all" control
                // as 'indeterminate'
                el.indeterminate = true;
            }
            $(this).parents("tr").removeClass('selected');
        } else {
            $(this).parents("tr").addClass('selected');
        }

        getCheckedCodes();
    });

    /*添加*/
    function add(w, h) {
        var index = layer.open({
            type: 2,
            title: "添加",
            content: "@ViewBag.AddUrl"
        });
        layer.full(index);
    }

    /*编辑*/
    function edit(code, w, h) {
        var index = layer.open({
            type: 2,
            title: "编辑",
            content: "@ViewBag.EditUrl" + "?code=" + code
        });
        layer.full(index);
    }

    /*详情*/
    function detail(code, w, h) {
        var index = layer.open({
            type: 2,
            title: "详情",
            content: "@ViewBag.DetailUrl" + "?code=" + code
        });
        layer.full(index);
    }

    /*删除*/
    function del(code) {
        layer.confirm('确认要删除吗？',
            function (index) {
                $.ajax({
                    type: 'POST',
                    url: '@ViewBag.DeleteUrl',
                    data: { code: code },
                    dataType: 'json',
                    success: function (data) {
                        // 刷新列表
                        if (data.res === "success") {
                            layer.msg('已删除!', { icon: 1, time: 1000 });
                        } else {
                            layer.msg(data.msg, { icon: 1, time: 1000 });
                        }
                        reload();
                    },
                    error: function (data) {
                        console.log(data.msg);
                    }
                });
            });
    }

    // 批量删除
    function dels() {
        layer.confirm('确认要批量删除吗？',
            function (index) {
                $.ajax({
                    type: 'POST',
                    url: '@ViewBag.DeletesUrl',
                    data: { code: $("codes").val() },
                    dataType: 'json',
                    success: function (data) {
                        // 刷新列表
                        if (data.res === "success") {
                            layer.msg('已删除!', { icon: 1, time: 1000 });
                        } else {
                            layer.msg(data.msg, { icon: 1, time: 1000 });
                        }
                    },
                    error: function (data) {
                        console.log(data.msg);
                    }
                });
            });
    }

    /*资讯-审核*/
    function article_shenhe(obj, id) {
        layer.confirm('审核文章？',
            {
                btn: ['通过', '不通过', '取消'],
                shade: false,
                closeBtn: 0
            },
            function () {
                $(obj)
                    .parents("tr")
                    .find(".td-manage")
                    .prepend('<a class="c-primary" onClick="article_start(this,id)" href="javascript:;" title="申请上线">申请上线</a>');
                $(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已发布</span>');
                $(obj).remove();
                layer.msg('已发布', { icon: 6, time: 1000 });
            },
            function () {
                $(obj)
                    .parents("tr")
                    .find(".td-manage")
                    .prepend('<a class="c-primary" onClick="article_shenqing(this,id)" href="javascript:;" title="申请上线">申请上线</a>');
                $(obj).parents("tr").find(".td-status").html('<span class="label label-danger radius">未通过</span>');
                $(obj).remove();
                layer.msg('未通过', { icon: 5, time: 1000 });
            });
    }

    /*资讯-下架*/
    function article_stop(obj, id) {
        layer.confirm('确认要下架吗？',
            function (index) {
                $(obj)
                    .parents("tr")
                    .find(".td-manage")
                    .prepend('<a style="text-decoration:none" onClick="article_start(this,id)" href="javascript:;" title="发布"><i class="Hui-iconfont">&#xe603;</i></a>');
                $(obj).parents("tr").find(".td-status").html('<span class="label label-defaunt radius">已下架</span>');
                $(obj).remove();
                layer.msg('已下架!', { icon: 5, time: 1000 });
            });
    }

    /*资讯-发布*/
    function article_start(obj, id) {
        layer.confirm('确认要发布吗？',
            function (index) {
                $(obj)
                    .parents("tr")
                    .find(".td-manage")
                    .prepend('<a style="text-decoration:none" onClick="article_stop(this,id)" href="javascript:;" title="下架"><i class="Hui-iconfont">&#xe6de;</i></a>');
                $(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已发布</span>');
                $(obj).remove();
                layer.msg('已发布!', { icon: 6, time: 1000 });
            });
    }

    /*资讯-申请上线*/
    function article_shenqing(obj, id) {
        $(obj).parents("tr").find(".td-status").html('<span class="label label-default radius">待审核</span>');
        $(obj).parents("tr").find(".td-manage").html("");
        layer.msg('已提交申请，耐心等待审核!', { icon: 1, time: 2000 });
    }
    </script>
    <!--/请在上方写此页面业务相关的脚本-->
}

@RenderBody()
@RenderSection("headHtml", false)

<section class="">
    <div class="">
        <article class="cl">
            <div class="mt-20">
                <input type="hidden" id="codes" name="codes" value="" />
                <table class="table table-border table-bordered table-bg table-hover table-sort" id="tb-@ViewBag.GridId" style="width: 100%;">
                    <thead>
                        <tr class="text-c">
                            @RenderSection("tableHeadHtml", true)
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>
        </article>
    </div>
</section>

@RenderSection("footHtml", false)